package com.telecom.cms.dao;
// default package

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.telecom.cms.dto.*;
import com.telecom.cms.util.Constant;

/**
 * Data access object (DAO) for domain model class CampMainMenu.
 * 
 * @see .CampMainMenu
 * @author MyEclipse Persistence Tools
 */

public class CampMainMenuDAO {
	private static List instlist=null;
	Dao dao = new DaoImpStd();

	String db = (new Constant()).getPara("DATABASE");

	public int insert(CampMainMenu transientInstance) {
		int rs = 0;
		String sql = "select * from  " + db + ".CAMP_MAIN_MENU where M_Menu_Id="
				+ transientInstance.getMMenuId();
		try {
			ResultSet rs2 = dao.select(sql);
			if (rs2.next())
				sql = "update " + db + ".CAMP_MAIN_MENU set M_Menu_Name='"
						+ transientInstance.getMMenuName() + "',M_Menu_Link='"
						+ transientInstance.getMMenuLink().replace("'", "''") + "',M_Menu_Left_State='" 
						+ transientInstance.getMMenuLeftState()+ "',M_Menu_Show_Order="
						+ transientInstance.getMMenuShowOrder()+ ",M_Menu_remark='"
						+ transientInstance.getMMenuRemark()+ "',M_Menu_Auth="
						+ Constant.EasyEnc((long)(transientInstance.getMMenuAuth()+transientInstance.getMMenuId()))
						+ ",M_Menu_Target='"+transientInstance.getMMenuTarget()+ "' "
						+ " where M_Menu_Id=" + transientInstance.getMMenuId();
			else
				sql = "insert into " + db + ".CAMP_MAIN_MENU values(" 
						+ transientInstance.getMMenuId()+",'"
				        + transientInstance.getMMenuName() + "', '"
						+ transientInstance.getMMenuLink().replace("'", "''") + "' , '"
						+ transientInstance.getMMenuLeftState()+ "',"
						+ transientInstance.getMMenuShowOrder() + ",'" 
						+ transientInstance.getMMenuRemark() + "'," 
						+ Constant.EasyEnc((long)(transientInstance.getMMenuAuth()+transientInstance.getMMenuId())) 
						+ ",'"+transientInstance.getMMenuTarget()+"')";
			rs = dao.insert(sql);
			if (rs2 != null) {
				rs2.close();
			}

		} catch (Exception re) {
			System.out.println("save failed:" + re);
		}
		finally
		{
			dao.closeconn();
		}
		findAllForce();
		return rs;
	}
	
	public void delete(CampMainMenu persistentInstance) {
		String sql = "delete from " + db + ".CAMP_MAIN_MENU where M_Menu_Id="
				+ persistentInstance.getMMenuId();
		try {
			dao.delete(sql);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		findAllForce();
	}
	
	public List findAllForce() {
		String sql = "select * from " + db + ".CAMP_MAIN_MENU order by M_Menu_Show_Order ";
		List list = new ArrayList();
		try {
			ResultSet rs = dao.select(sql);
			while (rs.next()) {
				CampMainMenu sd = new CampMainMenu();
				sd.setMMenuId(rs.getInt(1));
				sd.setMMenuName(rs.getString(2));
				sd.setMMenuLink(rs.getString(3));
				sd.setMMenuLeftState(rs.getString(4));
				sd.setMMenuShowOrder(rs.getInt(5));
				sd.setMMenuRemark(rs.getString(6));
				sd.setMMenuAuth((int)(Constant.EasyDec((long)rs.getInt(7))-rs.getInt(1)));
				sd.setMMenuTarget(rs.getString(8));
				if((int)(Constant.EasyDec((long)rs.getInt(7))-rs.getInt(1))<=Constant.CheckAuth())
				list.add(sd);
			}
			if (rs != null) {
				rs.close();
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			dao.closeconn();
		}
		instlist=list;
		return list;
	}
	
	public List findAll() {
		if(instlist==null)
			findAllForce();
		return instlist;
	}
	
	public CampMainMenu findById(int menuid) {
		//String sql = "select * from " + db + ".CAMP_MAIN_MENU where M_Menu_Id="+menuid;
		CampMainMenu sd = null;
		if(instlist==null)
			findAllForce();
		for(int i=0;i<instlist.size();i++)
		{
			sd = (CampMainMenu)instlist.get(i);
			if(sd.getMMenuId()==menuid)
				return sd;
		}
		/*try {
			ResultSet rs = dao.select(sql);
			if (rs.next()) {
				sd = new CampMainMenu();
				sd.setMMenuId(rs.getInt(1));
				sd.setMMenuName(rs.getString(2));
				sd.setMMenuLink(rs.getString(3));
				sd.setMMenuLeftState(rs.getString(4));
				sd.setMMenuShowOrder(rs.getInt(5));
				sd.setMMenuRemark(rs.getString(6));
				sd.setMMenuAuth((int)(Constant.EasyDec((long)rs.getInt(7))-rs.getInt(1)));
				sd.setMMenuTarget(rs.getString(8));
			}
			if (rs != null) {
				rs.close();
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			dao.closeconn();
		}*/
		return sd;
	}
}